package pl.un4giv3n.mightvsmagic.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import pl.un4giv3n.mightvsmagic.entity.User;

public class MailSender {
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "gra.mightvsmagic@gmail.com";
    private static final String SMTP_AUTH_PWD  = "Mv$M_th3G4m3";
	private static final String NEW_PASSWD_MESSAGE = "Witaj <<user>>!\n Zgodnie z prośbą wygenerowaliśmy dla ciebie nowe hasło.\nTwoje nowe hasło to <<password>>\nNie zapomnij zmienic go zaraz po zalogowaniu!\n pozdrawiam,\n Tygrys - deamon mailowy MvsM";
	public static final String NEW_PASSWD_SUBJECT = "MightVsMagic - nowe hasło";
	public static final String NEW_USER_SUBJECT = "Witamy w grze MightVsMagic";
	public static final String NEW_USER_MESSAGE = "Witaj <<user>>!\n To jest potwierdzenie twojej rejestracji w grze MightVsMagic. Zachowaj te dane, będą przydatne w razie problemów z logowaniem.\n Login:<<user>> \n UserId:<<userId>>\n pozdrawiam,\n Tygrys - deamon mailowy MvsM";
	public static final String BLOCK_USER_SUBJECT = "Konto zablokowane";
	public static final String UNBLOCK_USER_SUBJECT = "Konto odblokowane";
	public static final String BLOCK_USER_MESSAGE =  "Witaj <<user>>!\n Twoje konto w grze MightVsMagic \n Login:<<user>> \n UserId:<<userId>>\n zostało zablokowane przez administratora <<admin>>\n Powód blokady:\n<<reason>>\n pozdrawiam,\n Tygrys - deamon mailowy MvsM";
	public static final String UNBLOCK_USER_MESSAGE =  "Witaj <<user>>!\n Twoje konto w grze MightVsMagic \n Login:<<user>> \n UserId:<<userId>>\n zostało odblokowane przez administratora <<admin>>\n Powód odblokowania:\n<<reason>>\n pozdrawiam,\n Tygrys - deamon mailowy MvsM";
	public static final String DELETE_USER_SUBJECT = "Konto usunięte";
	public static final String DELETE_USER_MESSAGE =  "Witaj <<user>>!\n Twoje konto w grze MightVsMagic \n Login:<<user>> \n UserId:<<userId>>\n zostało usunięte na twoją prośbę \n pozdrawiam,\n Tygrys - deamon mailowy MvsM";
	
    
    public static String getNewPasswordMessage(String username, String password){
    	return NEW_PASSWD_MESSAGE.replaceAll("<<user>>", username).replaceAll("<<password>>", password);
    }
    public static String getNewUserMessage(String username, String userId){
    	return NEW_USER_MESSAGE.replaceAll("<<user>>", username).replaceAll("<<userId>>", userId);
    }
    public static String getBlockUserMessage(User toDelete, String userId, String adminLogin, String reason) {
		return BLOCK_USER_MESSAGE.replaceAll("<<user>>", toDelete.getLogin()).replaceAll("<<userId>>", userId).replaceAll("<<admin>>", adminLogin).replaceAll("<<reason>>", reason);
	}
    public static String getUnBlockUserMessage(User toDelete, String userId, String adminLogin, String reason) {
    	return UNBLOCK_USER_MESSAGE.replaceAll("<<user>>", toDelete.getLogin()).replaceAll("<<userId>>", userId).replaceAll("<<admin>>", adminLogin).replaceAll("<<reason>>", reason);
    }
    public static String getDeleteUserMessage(User toDelete, String userId) {
    	return DELETE_USER_MESSAGE.replaceAll("<<user>>", toDelete.getLogin()).replaceAll("<<userId>>", userId);
    }
    
	public static void send(String to,String subject, String text) throws NoSuchProviderException{
		
		 Properties props = new Properties();

	        props.put("mail.transport.protocol", "smtps");
	        props.put("mail.smtps.host", SMTP_HOST_NAME);
	        props.put("mail.smtps.auth", "true");
	        props.put("mail.smtps.quitwait", "false");

		
		Session mailSession = Session.getDefaultInstance(props);
        Transport transport = mailSession.getTransport();

		Message simpleMessage = new MimeMessage(mailSession);
		
		InternetAddress fromAddress = null;
		InternetAddress toAddress = null;
		try {
			fromAddress = new InternetAddress(SMTP_AUTH_USER);
			toAddress = new InternetAddress(to);
		} catch (AddressException e) {
			e.printStackTrace();
		}
		
		try {
			simpleMessage.setFrom(fromAddress);
			simpleMessage.setRecipient(RecipientType.TO, toAddress);
			simpleMessage.setSubject(subject);
			simpleMessage.setText(text);
			
			 transport.connect
	          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

	        transport.sendMessage(simpleMessage,
	        		simpleMessage.getRecipients(Message.RecipientType.TO));
	        transport.close();
	
		} catch (MessagingException e) {
			e.printStackTrace();
		}		
	}
	
	public static void main(String[] args) throws NoSuchProviderException {
		String to = "nadirowski@wp.pl";
		String subject = "Test";
		String message = "A test message";
		
		MailSender.send(to, subject, message);

	}
}
